#include "LinkSerialClient.h"
#include "PacketHexBase.h"
#include <iostream>
#include "lib.h"

using namespace controller;
void test_LinkSerialPort(void)
{
    // 1.测试字节流
    //  LinkSerialPort l;
    //  std::map<std::string, std::string> arg = {
    //      {"dev", "/dev/ttyv1"},
    //      //{"dev","/dev/ttyS0"},
    //      {"baud", "115200"}};
    //  l.open(arg);
    //  while (1)
    //  {
    //      if (l.is_open())
    //      {
    //          l.write((char *)"kaihua.yang", 11);
    //      }
    //      sleep(1);
    //  }

    // // 2. 测试 packet 协议
    // PacketHexBase packet_hex;
    // uint8_t data[100];
    // std::string str = "AB BA 00 16 00 00 00 02 00 00 00 00 00 00 00 01 00 00 00 00 31 01 01 00 A0 DD";
    // int s = LIB::hexStringToCharArray(str, data, sizeof(data));
    // PacketBase *p = packet_hex.on_get((unsigned char *)data, s);
    // if (p != NULL)
    // {
    //     std::cout << "接收到一个数据包" << std::endl;
    // }

    // 3.测试 串口 通讯packet
    PacketHexBase packet_hex;
    LinkSerialPort l;
    std::map<std::string, std::string> arg = {
        {"dev", "/dev/ttyv1"},
        //{"dev","/dev/ttyS0"},
        {"baud", "115200"}};
    l.set_dump_buffer_type(true);
    unsigned char s[4] = {0xAB, 0xBA, 0x12, 0x89};
    l.OnLinkRecv = [&](LinkBase *link, LinkBuffer *buf, const char *arg)
    {
        std::cout << "Packet Base" << std::endl;
        PacketBase *p = packet_hex.on_get((char *)buf->data(), buf->size());
        if (p != NULL)
        {
            std::cout << "接收到一个数据包" << std::endl;
        }
    };
    l.open(arg);
    while (1)
    {
        if (l.is_open())
        {
            l.write((char *)s, sizeof(s));
        }
        sleep(1);
    }
}
